package test;

import com.xiaoming.module2.quickstarter.dao.*;
import com.xiaoming.module2.quickstarter.entity.OrderUser;
import com.xiaoming.module2.quickstarter.entity.User;
import com.xiaoming.module2.quickstarter.entity.UserOrder;
import com.xiaoming.module2.quickstarter.entity.UserRole;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.javassist.bytecode.analysis.Executor;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class QuickStaterBootstrap {
    // 传统查找全部
    @Test
    public void findAllTest() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        List<User> users = sqlSession.selectList("user.findAll");
        for (User user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    // 传统新增
    @Test
    public void insertTest() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = new User();
        user.setAge(100);
        user.setId("66");
        user.setName("张三");
        sqlSession.insert("user.insert", user);
        // insert 、update 、 delete 一定要提交事务；
        // 或者设置自动提交  SqlSession sqlSession = sqlSessionFactory.openSession(true);
        sqlSession.commit();
        sqlSession.close();
    }

    // 代理模式下的查找
    @Test
    public void userDaoTest() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        List<User> users = userDao.findAll();
        System.out.println("dao");
        for (User user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    // 动态SQL findByCondition

    @Test
    public void dynamicSqlTest() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        DynamicSqlDao userDao = sqlSession.getMapper(DynamicSqlDao.class);

        User condition = new User();
        condition.setAge(100);
        condition.setId("400");
        condition.setName("张三");
        List<User> users = userDao.findByCondition(condition);
        System.out.println("----- dynamic sql -----");
        for (User user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    // 动态SQL foreach

    @Test
    public void dynamicSqlForEachTest() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        DynamicSqlDao userDao = sqlSession.getMapper(DynamicSqlDao.class);

        String[] ids = {"1","2"};
        List<User> users = userDao.findByIds(ids);
        System.out.println("----- dynamic sql -----");
        for (User user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }

    // 1对1 查询
    @Test
    public void multiTablefindOrdersTest() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrderUserMapper orderMapper = sqlSession.getMapper(OrderUserMapper.class);

        List<OrderUser> orders = orderMapper.findOrders();
        System.out.println("----- one mapper one -----");
        for (OrderUser order : orders) {
            System.out.println(order);
        }
        sqlSession.close();
    }

    // 1对多 查询
    @Test
    public void multiTablefindUsersTest() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserOrderMapper userOrderMapper = sqlSession.getMapper(UserOrderMapper.class);

        List<UserOrder> users = userOrderMapper.findUsers();
        System.out.println("----- one mapper multi -----");
        for (UserOrder user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }


    // 多对多 查询
    @Test
    public void multiTablefindAllTest() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        UserRoleMapper userRoleMapper = sqlSession.getMapper(UserRoleMapper.class);

        List<UserRole> users = userRoleMapper.findAll();
        System.out.println("----- one mapper multi -----");
        for (UserRole user : users) {
            System.out.println(user);
        }
        sqlSession.close();
    }




}
